%%%%%%%%%%%%%%%%QPSK%%%%%%%%%%%%%%%%

clear; clc;
%liczba bitów stosowanych w tecie
nbit=10000;
%wartociowoæ modulacji
M=4;
L=sqrt(M);

%How many pathways (Tx x Rx)
N=4;


%Entityattributevalue model
Eav=4;
%Input signal from 0 to 3
x_signal_input = randint(nbit,1,4);
%k parameter needed for normalization
k=sqrt((3*Eav)/(2*(M-1)));

%macierz wartoci nadawanych
Tx=[];

%Making of QPSK constelation
for i=1:nbit
    if x_signal_input(i,1) == 0
        Tx=[Tx (k+j*k)];
    elseif x_signal_input(i,1) == 1
        Tx=[Tx (-k+j*k)];
    elseif x_signal_input(i,1) == 2
        Tx=[Tx (k-j*k)];
    else
        Tx=[Tx (-k-j*k)];
    end;
end;

figure();
plot(real(Tx),imag(Tx),'ko','MarkerFaceColor','b');
title('QPSK INPUT');

%Gaussian noise
% 0 - noise power
% 1 - max value
y2 = wgn(nbit,1,0,'complex');

%RAND NOSIE
noise=randn(1,N*nbit)+j*randn(1,N*nbit);
%SIGNAL TO NOISE RATIO
SNR  = 10;

No = 10^(-SNR/10);

%Adding scaled noise to input signal
Tx = repmat(Tx,1,N);
Rx=Tx+sqrt(No/2).*noise;

%Apply Rice channel propagation model
sigma = std(Tx) ; %scattered mean power
%A = mean(Tx); %Amplituda składowej dominującej
A=Tx;
I0=besseli(0,(A.*Rx/(sigma).^2));
Rx=(Rx/(sigma).^2) .* exp(-((Rx).^2 + A.^2)/(2*sigma.^2)) .* I0;
%Rx=(Rx/(sigma).^2) .* exp((-(Rx).^2 + A.^2)./(2*sigma.^2)) .* I0;


%OUTPUT RES
figure();
plot(real(Rx),imag(Rx),'ko','MarkerFaceColor','b');
title('QPSK OUTPUT');



sym1=zeros(1,length(Rx)/4);
sym2=zeros(1,length(Rx)/4);
sym3=zeros(1,length(Rx)/4);
sym4=zeros(1,length(Rx)/4);


for i=1:(length(Rx)/N)
    for j=0:(N-1)
        j
        if(real(Rx(i+j*(length(Rx)/4)))<0 && imag(Rx(i+j*(length(Rx)/4))>0))
            sym1(1,i)=sym1(1,i)+1;
        end
        if(real(Rx(i+j*(length(Rx)/4)))>0 && imag(Rx(i+j*(length(Rx)/4))>1))
            sym2(1,i)=sym2(1,i)+1;
        end
        if(real(Rx(i+j*(length(Rx)/4)))<0 && imag(Rx(i+j*(length(Rx)/4))<0))
            sym3(1,i)=sym3(1,i)+1;
        end
        if(real(Rx(i+j*(length(Rx)/4)))>0 && imag(Rx(i+j*(length(Rx)/4))<0))
            sym4(1,i)=sym4(1,i)+1;
        end
    end
end


